//
//  LXDSongListViewController.m
//  BLDMusics
//
//  Created by lxd on 16/3/20.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDSongListViewController.h"


@interface LXDSongListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIVisualEffectView *visualEfView; /**< 毛玻璃 */

@property (nonatomic, retain) UITableView *tableView;

/**黑色边栏*/
@property (nonatomic, retain) UIView *backView;

/**上面的歌手图片*/
@property (nonatomic, retain) UIImageView *imagView;

/**红色小条*/
@property (nonatomic, retain) UIView *redView;

@end

@implementation LXDSongListViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ground.jpg"]];
     self.navigationController.navigationBar.translucent = YES;
        [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor colorWithRed:95 / 255.0 green:217 / 255.0 blue:238 / 255.0 alpha:1] colorWithAlphaComponent:0]] forBarMetrics:UIBarMetricsDefault];
    // 毛玻璃
    self.visualEfView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    _visualEfView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    _visualEfView.alpha = 0.8;
    _visualEfView.userInteractionEnabled = YES;
    [self.view addSubview:_visualEfView];

    [self createTableView];

}


#pragma mark - 创建collection
- (void)createTableView {
    
    NSArray *textStr = @[@"单曲", @"专辑", @"MV", @"歌手信息"];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGTH / 2, SCREEN_WIDTH, SCREEN_HEIGTH / 2 - 49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentOffset = CGPointMake(0, -10);
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.rowHeight = SCREEN_HEIGTH / 2 - 49;
    [_tableView registerClass:[LXDSingerTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 头视图
    self.imagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH / 2)];
    
    [_imagView sd_setImageWithURL:[NSURL URLWithString:_singer.pic_url]];
    [self.view addSubview:_imagView];
    _imagView.userInteractionEnabled  = YES;
    
    // 黑色边栏加在imagView上
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, _imagView.frame.size.height - 40, SCREEN_WIDTH, 40)];
    [_imagView addSubview:_backView];
    _backView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:0.8];
    _backView.userInteractionEnabled = YES;
    // 创建上面的button
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * (SCREEN_WIDTH / 2) , 0, (SCREEN_WIDTH / 2), _backView.frame.size.height);
        button.tag = 1000 + i;
        NSString *str = textStr[i];
        [button setTitle:str forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [_backView addSubview:button];
        [button addTarget:self action:@selector(buttonAction: changeView:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.redView = [[UIView alloc] initWithFrame:CGRectMake(0, _backView.frame.size.height - 3, SCREEN_WIDTH / 2, 3)];
    [_backView addSubview:_redView];
    _redView.backgroundColor = [UIColor redColor];
    // 添加通知，让collection移动的时候， redView也跟着动
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFrame:) name:@"红色小条" object:nil];
    
    
    // 取消navigationbar下面的一条素的黑线
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        NSArray *list = self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView = (UIImageView *)obj;
                NSArray *list2 = imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2 = (UIImageView *)obj2;
                        imageView2.hidden = YES;
                    }
                }
            }
        }
    }
}


#pragma mark - 点击button的方法
- (void)buttonAction:(UIButton *)button changeView:(UIView *)view {
    // 发送通知，通过点击button去改变collection的偏移量
    [[NSNotificationCenter defaultCenter] postNotificationName:@"偏移量" object:button];
    _redView.frame = CGRectMake(SCREEN_WIDTH / 2 * (button.tag - 1000),_backView.frame.size.height - 3, SCREEN_WIDTH / 2, 3);
}

#pragma mark - 红色小条改变位置的方法
- (void)changeFrame:(NSNotification *)notification {
    UICollectionView *collection = notification.object;
    _redView.frame = CGRectMake(collection.contentOffset.x / SCREEN_WIDTH * (_backView.frame.size.width / 2), _backView.frame.size.height - 3, SCREEN_WIDTH / 2, 3);
}


- (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文获取图片
    UIImage *theImag = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    return theImag;
}




#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LXDSingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.ID = _singer.singer_id;
    return cell;
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
